import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:glorysoft_mobile_plugin/tool/http/http_client.dart';
import 'package:glorysoft_mobile_plugin/tool/utils/shared_preferences_util.dart';
import 'package:quick_code_platform/constant/constant.dart';
import 'package:quick_code_platform/constant/quick_initial.dart';
import 'package:quick_code_platform/entity/enum_entity.dart';
import 'package:quick_code_platform/http/http_service.dart';
import '../base/base_controller.dart';

class SettingController extends BaseController {
  TextEditingController? address;
  TextEditingController? timeout;
  TextEditingController? filePath;
  TextEditingController? formId;
  TextEditingController? itfId;
  TextEditingController? eqpId;
  String label = "label";
  String value = "value";
  List<Map<dynamic, dynamic>> mShowList = [];
  Map<dynamic, dynamic>? selectPageAddress;
  Map<dynamic, dynamic>? selectInterfaceAddress;
  bool isSetting = true;

  @override
  void onInit() {
    super.onInit();
    isSetting = Get.arguments?['isSetting'] ?? true;
    address = TextEditingController(text: SpUtil.getString('quick_address'));
    if (address!.text.isEmpty) {
      address?.text = "http://0.0.0.0:8080";
    }
    timeout =
        TextEditingController(text: SpUtil.getString('quick_timeout') ?? "30");
    if (timeout!.text.isEmpty) {
      timeout?.text = "30";
    }
    formId = TextEditingController(text: SpUtil.getString('enter_formId'));
    itfId = TextEditingController(
        text: SpUtil.getString('interface_address') ?? "http://0.0.0.0:8080");
    if (itfId!.text.isEmpty) {
      itfId?.text = "http://0.0.0.0:8080";
    }
    filePath = TextEditingController(text: SpUtil.getString('quick_catalogue'));
    eqpId = TextEditingController(text: SpUtil.getString('eqpId'));
    // getShowList();
  }

  // void getShowList() {
  //   mShowList.add({label: "http://", value: "http://"});
  //   mShowList.add({label: "https://", value: "https://"});
  //   if ((address?.text ?? "").startsWith("https://")) {
  //     selectPageAddress = mShowList[1];
  //   } else {
  //     selectPageAddress = mShowList[0];
  //   }
  //   if ((itfId?.text ?? "").startsWith("https://")) {
  //     selectInterfaceAddress = mShowList[1];
  //   } else {
  //     selectInterfaceAddress = mShowList[0];
  //   }
  // }

  @override
  void onClose() {
    super.onClose();
    canelToken?.cancel();
  }

  saveAllConfig() {
    String addressStr = address?.text ?? "";
    if (addressStr.isEmpty ||
        !(addressStr.startsWith("http://") || addressStr.startsWith("https://"))) {
      showToast("请输入正确的文件地址");
    }

    String itfIdStr = itfId?.text ?? "";
    if (itfIdStr.isEmpty ||
        !(itfIdStr.startsWith("http://") || itfIdStr.startsWith("https://"))) {
      showToast("请输入正确的接口请求地址");
    }
    String filePathStr = filePath?.text ?? "";
    // if (filePathStr.isEmpty || !itfIdStr.startsWith("/")) {
    //   showToast("请输入正确的文件目录");
    // }

    String timeOutStr = timeout?.text ?? "";
    if (timeOutStr.isEmpty || timeOutStr == "0") {
      showToast("请输入正确接口超时时间");
    }
    initialQuickConfig(
        address: addressStr,
        timeout: timeOutStr,
        catalogue: filePathStr,
        itfAddress: itfIdStr,
        modeType: RunModeType.local);
    saveUpdateApp(isSetting: isSetting);
  }

  savePageConfig() {
    SpUtil.putString('quick_address', address?.text ?? '');
  }

  saveFilePath() {
    SpUtil.putString('quick_catalogue', address?.text ?? '');
  }

  saveInterFaceConfig() {
    SpUtil.putString('interface_address', itfId?.text ?? '');
  }

  saveTimeout() async {
    if (timeout?.text.isNotEmpty ?? false) {
      SpUtil.putString('quick_timeout', timeout?.text ?? '30');
      Get.delete<HttpClient>(force: true);
      await Get.putAsync(() => HttpService().init(), permanent: true);
    }
  }

  saveFormId() {
    SpUtil.putString('enter_formId', formId?.text ?? '');
  }

  saveEqpId() {
    SpUtil.putString('eqpId', eqpId?.text ?? '');
  }

  saveIsFailingDialog(bool value) {
    SpUtil.putBool('isFailingDialog', value);
    update();
  }
}
